package cc.fs.study.drawandevent;

import android.content.Context;
import android.util.AttributeSet;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.HorizontalScrollView;
import android.widget.ScrollView;

public class VScrollView extends ScrollView {

    View childHScrollView;

    public VScrollView(Context context) {
        super(context);
    }

    public VScrollView(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    public VScrollView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

    @Override
    public boolean dispatchTouchEvent(MotionEvent ev) {
        return super.dispatchTouchEvent(ev);
    }

    int mLastXIntercept = 0;
    int mLastYIntercept = 0;

    @Override
    public boolean onInterceptTouchEvent(MotionEvent ev) {

        if(childHScrollView == null ){
            ViewGroup rootView = (ViewGroup) getChildAt(0);
            for(int i=0;i<rootView.getChildCount();i++){
                if(rootView.getChildAt(i) instanceof HorizontalScrollView){
                    childHScrollView = rootView.getChildAt(i);
                }
            }
        }

        boolean isIntercept = false;
        int x = (int) ev.getX();
        int y = (int) ev.getY();

        switch (ev.getAction()) {
            case MotionEvent.ACTION_DOWN:
                isIntercept = false;
                break;

            case MotionEvent.ACTION_MOVE:
                if (Math.abs(x - mLastXIntercept) >= Math.abs(y - mLastYIntercept)) {
                    isIntercept = false;
                } else {
                    isIntercept = true;
                }
                break;

            case MotionEvent.ACTION_UP:
                isIntercept = false;
                break;

            default:
                break;
        }

        e("VScrollView width: " + Math.abs(x - mLastXIntercept) + "  height:"+Math.abs(y - mLastYIntercept));

        mLastXIntercept = x;
        mLastYIntercept = y;
        e("截取事件"+isIntercept);
        return super.onInterceptTouchEvent(ev);
    }

    @Override
    public boolean onTouchEvent(MotionEvent ev) {
        e("处理滑动事件");
        return super.onTouchEvent(ev);
    }

    public void e(String msg) {
        Log.e(this.getClass().getSimpleName(), msg);
    }
}
